#include "precedence_stack.h"

void init_precedence_stack(precedence_stack *stack, uint32_t precedence_stack_alloc_size)
{
  stack->stack = (token_node**) malloc(sizeof(token_node*)*precedence_stack_alloc_size);
  stack->ceil = precedence_stack_alloc_size;
  stack->tos = 0;
#if TEST
  stack->max = 0;
#endif
}

void push_precedence_node_on_stack(precedence_stack *stack, token_node *token, uint32_t precedence_stack_realloc_size)
{
  if (stack->tos >= stack->ceil) {
    stack->stack = (token_node**) realloc(stack->stack, sizeof(token_node*)*(stack->ceil + precedence_stack_realloc_size));
    stack->ceil += precedence_stack_realloc_size;
  }
  stack->stack[stack->tos] = token;
  ++stack->tos;
#if TEST
  if (stack->max < stack->tos) {
    stack->max = stack->tos;
  }
#endif
}

token_node *pop_precedence_node_from_stack(precedence_stack *stack)
{
  --stack->tos;
  return stack->stack[stack->tos];
}

token_node *get_precedence_stack_node_at_index(precedence_stack *stack, uint32_t index)
{
  if (index >= stack->tos) {
    return NULL;
  }
  return stack->stack[index];
}

void free_precedence_stack(precedence_stack *stack)
{
  free(stack->stack);
}

